<template>
  <div class="main">
    <div class="form_content">
      <NScrollbar>
        <NSpin :show="spinShow">
          <div class="content">
            <div class="topTitle">线索信息</div>
            <div class="basin-info">
              <div style="display: flex; align-items: center">
                <div class="basin-left">
                  <div class="box">
                    <span>线索名称：</span>
                    <span>{{ dispatchInfo.clueName }}</span>
                  </div>
                  <div class="box">
                    <span>接收人：</span>
                    <span>{{ dispatchInfo.receiveUser }}</span>
                  </div>
                  <div class="box">
                    <span>姓名：</span>
                    <span>{{ dispatchInfo.userName }}</span>
                  </div>
                  <div class="box">
                    <span>联系方式：</span>
                    <span>{{ dispatchInfo.phone }}</span>
                  </div>
                  <div class="box">
                    <span>证件类型：</span>
                    <span>{{ dispatchInfo.idTypeName }}</span>
                  </div>
                </div>
                <div class="basin-right">
                  <div class="box">
                    <span>移交人：</span>
                    <span>{{ dispatchInfo.transferUser }}</span>
                  </div>
                  <div class="box">
                    <span>接收时间：</span>
                    <span>{{ dispatchInfo.receiveTime }}</span>
                  </div>
                  <div class="box">
                    <span>单位：</span>
                    <span>{{ dispatchInfo.dept }}</span>
                  </div>
                  <div class="box">
                    <span>提交时间：</span>
                    <span>{{ dispatchInfo.commitTime }}</span>
                  </div>
                  <div class="box">
                    <span>证件号：</span>
                    <span>{{ dispatchInfo.idNo }}</span>
                  </div>
                </div>
              </div>
            </div>
            <div class="topTitle">物品信息</div>
            <div class="table">
              <NDataTable
                :bordered="false"
                :columns="columns"
                :data="tableData"
                :loading="tableLoading"
                :pagination="false"
                striped
                :rowKey="rowKey"
              />
              <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
            </div>
          </div>
        </NSpin>
      </NScrollbar>
    </div>
  </div>
  <FixedAction>
    <NSpace justify="start">
      <NButton
        style="width: 78px; color: #5fb17f"
        ghost
        @click="routeReplace"
      >
        <template #icon>
          <NImage
            :src="VBackImg"
            style="width: 50rem; height: 50rem"
            preview-disabled
          ></NImage>
        </template>
        返回
      </NButton>
    </NSpace>
  </FixedAction>
</template>
<script setup lang="ts">
import {
  NSpace,
  NButton,
  NScrollbar,
  NDataTable,
  useMessage,
  NSpin,
  NPagination,
  NImage
} from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import router from '@/router'
import { useRoute } from 'vue-router'
import { ref, watch, h, reactive } from 'vue'
import { useTagStore } from '@/stores/layout'
import FixedAction from '@/components/FixedAction.vue'
import { sendRequest } from '@/utils/api'
import type { HttpResponse, HttpResponsePagination } from '@/utils/http'
import type { clueModel } from '@/api/clue'
import VBackImg from '@/assets/images/back.png'
import useFlush from '@/hooks/useFlush'
import useRouteReplace from '@/hooks/useRouteReplace'

const tags = useTagStore().tags
const route = useRoute()
const routeReplace = useRouteReplace(route.query.redirect as string)
useFlush('flushClueTable', () => queryTableData())
const message = useMessage()
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  showSizePicker: true,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  onChange: (page: number) => {
    paginationReactive.page = page
    queryTableData()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    queryTableData()
  }
})
const rowKey: any = (row: any) => row.id || ''
const columns: DataTableColumns = [
  {
    title: '征集品名称',
    key: 'itemName',
    width: 120
  },
  {
    title: '类型',
    key: 'itemTypeName',
    width: 100
  },
  {
    title: '征集方式',
    key: 'collectTypeName',
    width: 100
  },
  {
    title: '接收时间',
    key: 'receiveTime',
    width: 100
  },
  {
    title: '接收人',
    key: 'receiveUser',
    width: 100
  },
  {
    title: '数量',
    key: 'itemNum',
    width: 100
  },
  {
    title: '操作',
    key: 'action',
    width: 100,
    align: 'center',
    fixed: 'right',
    render: (e: any) => [
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: () => handleDetail(e)
        },
        {
          default: () => '详情'
        }
      )
    ]
  }
]

// 返回
const goBack = () => {
  const index = tags.findIndex((item: any) => item.path === router.currentRoute.value.fullPath)
  
    tags.splice(index, 1)
if (tags.length == 0) {
    router.push({ path: '/OA/Workbench/UserDesktop' })
  } else history.back()
}
// 获取详情
const spinShow = ref(false)
const dispatchInfo = ref<any>({})
const getDispatchInfo = async () => {
  spinShow.value = true
  const [error, res] = await sendRequest<HttpResponse<clueModel>>({
    module: 'clue',
    api: 'baseInfo',
    uri: {
      id: route.params.id as never
    }
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  // @ts-ignore
  dispatchInfo.value = res.data
  // @ts-ignore
  // tableData.value = res.data.collectList ? res.data.collectList : []
  spinShow.value = false
}
const tableLoading = ref(false)
const tableData = ref<clueModel[]>([])
const queryTableData = async () => {
  tableLoading.value = true
  const [error, res] = await sendRequest<HttpResponsePagination<clueModel>>({
    module: 'clue',
    api: 'listItemsByClueId',
    override: {
      params: {
        id: route.params.id,
        page: paginationReactive.page,
        limit: paginationReactive.pageSize
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  paginationReactive.itemCount = res.data.totalCount
  tableData.value = res.data.list
}
const handleDetail = (e: any) => {
  router.push({
    name: 'ClueOrganizationDetail',
    params: { id: e.id },
    query: {
      type: 'detail',
      redirect: route.fullPath
    }
  })
}

watch(
  () => route,
  () => {
    if (route.name == 'clueManagementDetail') {
      getDispatchInfo()
      queryTableData()
    }
  },
  { deep: true, immediate: true }
)
</script>
<style scoped lang="scss">
@mixin CommonFont {
  font-size: 18rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: #333333;
}

.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .form_content {
    width: 100%;
    height: 100%;
    padding: 20rem 20rem 0;

    .topTitle {
      font-size: 22px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      color: #5fb17f;
      margin-bottom: 10rem;
    }

    .content {
      display: flex;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 18rem 33rem 120rem;
      overflow-y: scroll;
      background-color: #ebeceb;
      @include CommonFont;

      .basin-info {
        display: flex;
        flex-direction: column;
        width: 100%;
        height: auto;
        border-bottom: 1px dashed #bc9e70;
        margin-bottom: 20rem;

        .basin-left {
          flex: 0 0 40%;
        }

        .box {
          display: flex;
          margin-bottom: 20rem;
          font-size: 16rem;

          span:first-child {
            display: inline-block;
            width: 120rem;
          }

          span:last-child {
            display: inline-block;
            flex: 1;
          }
        }
      }

      .approve {
        display: flex;
        width: 100%;
        margin-bottom: 20rem;
        border-bottom: 1px dashed #bc9e70;

        .box {
          display: flex;
          flex-direction: column;
          justify-content: center;
          min-width: 100rem;
          width: auto;
          height: 90rem;
          border-radius: 4px;
          border: 1px solid #d9d9d9;
          padding: 12rem;
          margin-right: 10rem;
          margin-bottom: 20rem;
        }
      }

      .table {
        width: 80%;
        background-color: #ebeceb;
      }
    }
  }
}

.content::-webkit-scrollbar {
  display: none;
}

:deep(.n-spin-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-content) {
  width: 100%;
  height: 100%;
}

:deep(.n-spin-content) {
  width: 100%;
  height: 100%;
}
</style>
